package engineer.serverapp.classes;

import java.awt.Desktop;
import java.io.File;
import java.io.FileOutputStream;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import com.itextpdf.text.Anchor;
import com.itextpdf.text.BadElementException;
import com.itextpdf.text.Chapter;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;

import com.itextpdf.text.Paragraph;

import com.itextpdf.text.Section;

import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import engineer.serverapp.tools.Connector;

public class CreatePDF {
	

	private static final String RAPORT_DLA_WSZYSTKICH_AUTOMATOW = "c:/temp/Raport_dla_wszystkich_automatow.pdf";
	private static final String RAPORT_DLA_PUSTYCH_AUTOMATOW = "c:/temp/Raport_dla_pustych_automatow.pdf";
	private static final String RAPORT_DLA_WYBRANEGO_AUTOMATU = "c:/temp/Raport_dla_wybranego_automatu.pdf";






	private static void addMetaData(Document document) {
		document.addTitle("Raport dotyczący automatów");
		document.addSubject("Automaty Sprzedające");
		document.addKeywords("Automaty, PDF");
		document.addAuthor("AutomatClientApp");
		document.addCreator("AutomatClientApp");
	}

	private static void addEmptyLine(Paragraph paragraph, int number) {
		for (int i = 0; i < number; i++) {
			paragraph.add(new Paragraph(" "));
		}
	}

	private static void addTitlePage(Document document, int userID, String type)
			throws DocumentException {
		
		try {
			BaseFont bf = BaseFont.createFont(BaseFont.TIMES_ROMAN,  
	                    BaseFont.CP1250, BaseFont.EMBEDDED);  
	        Font catFont = new Font(bf, 36, Font.BOLD); 
	        Font smallBold = new Font(bf, 12,
	    			Font.BOLD);
			Connection connection = new Connector().getConnection();
			Statement selectStatement = connection.createStatement();
			String query = "select name, surname from automats.users where id="
					+ userID;
			ResultSet rs = selectStatement.executeQuery(query);
			while (rs.next()) {
				String name = rs.getString("name");
				String surname = rs.getString("surname");
				String username = name + " " + surname;
				Paragraph preface = new Paragraph();
				
				SimpleDateFormat simpleDateHere = new SimpleDateFormat(
						"yyyy-MM-dd kk:mm:ss");
				Paragraph raport = new Paragraph("Raport wygenerowano: "
						+ simpleDateHere.format(new Date()), smallBold);
				raport.setAlignment(Element.ALIGN_LEFT);
				addEmptyLine(raport, 1);
				raport.add(new Paragraph("Dla: " + username, smallBold));

				addEmptyLine(preface, 15);
				if (type.equals("all")){
					preface.add(new Paragraph("Automaty Sprzedające",
						catFont));
			
					addEmptyLine(preface, 2);
					preface.add(new Paragraph("Raport Dla Wszystkich Automatów",
						catFont));
					preface.setAlignment(Element.ALIGN_CENTER);
				}else if(type.equals("empty")){
					preface.add(new Paragraph("Automaty Sprzedające",
							catFont));
					
					addEmptyLine(preface, 2);
					preface.add(new Paragraph("Raport Dla Braków w  Automatach",
						catFont));
					preface.setAlignment(Element.ALIGN_CENTER);
				}else if(type.equals("selected")){
					preface.add(new Paragraph("Automaty Sprzedające",
							catFont));
			
					addEmptyLine(preface, 2);
					preface.add(new Paragraph("Raport Dla Wybranego Automatu",
						catFont));
					preface.setAlignment(Element.ALIGN_CENTER);
				}
				addEmptyLine(preface, 1);
				preface.add(new Paragraph(
						"Raport wygenerowany na potrzeby użytkownika ",
						smallBold));
				document.add(raport);
				document.add(preface);
				// Start a new page
				document.newPage();

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static void createReportOfAllMachines(Section subCatPart)
			throws BadElementException {
		try{
			BaseFont bf = BaseFont.createFont(BaseFont.TIMES_ROMAN,  
	                    BaseFont.CP1250, BaseFont.EMBEDDED);  
			Font tables = new Font(bf, 12, Font.BOLD);
			Font tablesN = new Font(bf, 12, Font.NORMAL);
		PdfPTable table = new PdfPTable(5);
		
		PdfPTable tableEmpty = new PdfPTable(4);

		PdfPCell c2 = new PdfPCell(new Paragraph("Nazwa Automatu", tables));
		c2.setHorizontalAlignment(Element.ALIGN_CENTER);
		tableEmpty.addCell(c2);

		c2 = new PdfPCell(new Paragraph("Towar", tables));
		c2.setHorizontalAlignment(Element.ALIGN_CENTER);
		tableEmpty.addCell(c2);

		c2 = new PdfPCell(new Paragraph("Braki", tables));
		c2.setHorizontalAlignment(Element.ALIGN_CENTER);
		tableEmpty.addCell(c2);

		c2 = new PdfPCell(new Paragraph("Braki łączne", tables));
		c2.setHorizontalAlignment(Element.ALIGN_CENTER);
		tableEmpty.addCell(c2);

		tableEmpty.setHeaderRows(1);

		PdfPCell c1 = new PdfPCell(new Paragraph("Nazwa Automatu", tables));
		c1.setHorizontalAlignment(Element.ALIGN_CENTER);

		table.addCell(c1);

		c1 = new PdfPCell(new Paragraph("Adres Automatu", tables));
		c1.setHorizontalAlignment(Element.ALIGN_CENTER);
		table.addCell(c1);

		c1 = new PdfPCell(new Paragraph("Towary", tables));
		c1.setHorizontalAlignment(Element.ALIGN_CENTER);
		table.addCell(c1);

		c1 = new PdfPCell(new Paragraph("Stan", tables));
		c1.setHorizontalAlignment(Element.ALIGN_CENTER);
		table.addCell(c1);
		
		c1 = new PdfPCell(new Paragraph("Pojemność", tables));
		c1.setHorizontalAlignment(Element.ALIGN_CENTER);
		table.addCell(c1);

		table.setHeaderRows(1);
		ArrayList<Integer> current_amount = new ArrayList<Integer>();
		ArrayList<Integer> max_amount = new ArrayList<Integer>();
		try {
			Connection connection = new Connector().getConnection();
			Statement statement = connection.createStatement();
			Statement statement2 = connection.createStatement();
			ResultSet result = statement
					.executeQuery("SELECT * FROM automats.machines order by id");

			while (result.next()) {
				String name = result.getString("machines.name");
				String address = result.getString("machines.address");

				int id = Integer.parseInt(result.getString("machines.id"));

				ResultSet result2 = statement2
						.executeQuery("select machines_trademarks.current_amount, machines_trademarks.max_amount, dictionary_trademarks.name, machines_trademarks.machine_id from automats.machines_trademarks join automats.dictionary_trademarks on machines_trademarks.trademark_id=dictionary_trademarks.id where machines_trademarks.machine_id="
								+ id);
				while (result2.next()) {

					current_amount.add(result2
							.getInt("machines_trademarks.current_amount"));
					max_amount.add(result2
							.getInt("machines_trademarks.max_amount"));
				}
				PdfPCell names = new PdfPCell(new Paragraph(name, tablesN));
				table.addCell(names);

				PdfPCell addresses = new PdfPCell(new Paragraph(address, tablesN));
				table.addCell(addresses);
				PdfPCell trademarks = new PdfPCell(new Paragraph(
						"Coca-Cola\nFanta\nSprite\nNestea\nVitel", tablesN));
				trademarks.setHorizontalAlignment(Element.ALIGN_CENTER);
				table.addCell(trademarks);
				PdfPCell values = new PdfPCell(new Paragraph(current_amount.get(0)
						+ "\n" + current_amount.get(1) + "\n"
						+ current_amount.get(2) + "\n" + current_amount.get(3)
						+ "\n" + current_amount.get(4), tablesN));
				values.setHorizontalAlignment(Element.ALIGN_CENTER);
				table.addCell(values);

				PdfPCell maximum = new PdfPCell(new Paragraph(max_amount.get(0)
						+ "\n" + max_amount.get(1) + "\n"
						+ max_amount.get(2) + "\n" + max_amount.get(3)
						+ "\n" + max_amount.get(4), tablesN));
				maximum.setHorizontalAlignment(Element.ALIGN_CENTER);
				table.addCell(maximum);
				
				tableEmpty.addCell(names);
				tableEmpty.addCell(trademarks);
				ArrayList<Integer> empty = new ArrayList<Integer>();
				for (int i = 0; i < 5; i++) {
					int max = (int) max_amount.get(i);
					int min = (int) current_amount.get(i);
					int wynik = max - min;
					empty.add(wynik);
				}
				PdfPCell empties = new PdfPCell(new Paragraph(empty.get(0) + "\n"
						+ empty.get(1) + "\n" + empty.get(2) + "\n"
						+ empty.get(3) + "\n" + empty.get(4),tablesN));
				empties.setHorizontalAlignment(Element.ALIGN_CENTER);
				tableEmpty.addCell(empties);
				int emptyAll = empty.get(0) + empty.get(1) + empty.get(2)
						+ empty.get(3) + empty.get(4);
				String temp = String.valueOf("\n\n" + emptyAll);
				PdfPCell emptiesSum = new PdfPCell(new Paragraph(temp, tablesN));
				emptiesSum.setHorizontalAlignment(Element.ALIGN_CENTER);
				tableEmpty.addCell(emptiesSum);

				current_amount.clear();
				max_amount.clear();
				empty.clear();

			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	
        Font subFont = new Font(bf, 16, Font.BOLD); 
			
		Paragraph subPara = new Paragraph("Stany Automatów", subFont);
		addEmptyLine(subPara, 2);
		subCatPart.add(subPara);
		subCatPart.add(table);
		Paragraph space = new Paragraph("", subFont);
		addEmptyLine(space, 11);
		subCatPart.add(space);
		Paragraph subParas = new Paragraph("Braki Automatów", subFont);
		addEmptyLine(subParas, 2);
		subCatPart.add(subParas);
		subCatPart.add(tableEmpty);
		}catch(Exception e){
			e.printStackTrace();
		}

	}

	
	  private static void createReportOfSelected(Section subCatPart, int machineID) throws
	 BadElementException { 
		  try{
				 BaseFont bf = BaseFont.createFont(BaseFont.TIMES_ROMAN,  
		                 BaseFont.CP1250, BaseFont.EMBEDDED);   
				 Font tables = new Font(bf, 12, Font.BOLD);
				 Font tablesN = new Font(bf, 12, Font.NORMAL);
			PdfPTable table = new PdfPTable(5);
			PdfPTable tableEmpty = new PdfPTable(4);

			PdfPCell c2 = new PdfPCell(new Paragraph("Nazwa Automatu", tables));
			c2.setHorizontalAlignment(Element.ALIGN_CENTER);
			tableEmpty.addCell(c2);

			c2 = new PdfPCell(new Paragraph("Adres", tables));
			c2.setHorizontalAlignment(Element.ALIGN_CENTER);
			tableEmpty.addCell(c2);

			c2 = new PdfPCell(new Paragraph("Braki", tables));
			c2.setHorizontalAlignment(Element.ALIGN_CENTER);
			tableEmpty.addCell(c2);

			c2 = new PdfPCell(new Paragraph("Braki łączne", tables));
			c2.setHorizontalAlignment(Element.ALIGN_CENTER);
			tableEmpty.addCell(c2);
			
			
			
			tableEmpty.setHeaderRows(1);

			PdfPCell c1 = new PdfPCell(new Paragraph("Nazwa Automatu", tables));
			c1.setHorizontalAlignment(Element.ALIGN_CENTER);

			table.addCell(c1);

			c1 = new PdfPCell(new Paragraph("Adres Automatu", tables));
			c1.setHorizontalAlignment(Element.ALIGN_CENTER);
			table.addCell(c1);

			c1 = new PdfPCell(new Paragraph("Towary", tables));
			c1.setHorizontalAlignment(Element.ALIGN_CENTER);
			table.addCell(c1);

			c1 = new PdfPCell(new Paragraph("Stan", tables));
			c1.setHorizontalAlignment(Element.ALIGN_CENTER);
			table.addCell(c1);
			
			c1 = new PdfPCell(new Paragraph("Pojemność", tables));
			c1.setHorizontalAlignment(Element.ALIGN_CENTER);
			table.addCell(c1);

			table.setHeaderRows(1);
			ArrayList<Integer> current_amount = new ArrayList<Integer>();
			ArrayList<Integer> max_amount = new ArrayList<Integer>();
			try {
				Connection connection = new Connector().getConnection();
				Statement statement = connection.createStatement();
				Statement statement2 = connection.createStatement();
				ResultSet result = statement
						.executeQuery("SELECT * FROM automats.machines where id="+machineID+" order by id");

				while (result.next()) {
					String name = result.getString("machines.name");
					String address = result.getString("machines.address");

					int id = Integer.parseInt(result.getString("machines.id"));

					ResultSet result2 = statement2
							.executeQuery("select machines_trademarks.current_amount, machines_trademarks.max_amount, dictionary_trademarks.name, machines_trademarks.machine_id from automats.machines_trademarks join automats.dictionary_trademarks on machines_trademarks.trademark_id=dictionary_trademarks.id where machines_trademarks.machine_id="
									+ id);
					while (result2.next()) {

						current_amount.add(result2
								.getInt("machines_trademarks.current_amount"));
						max_amount.add(result2
								.getInt("machines_trademarks.max_amount"));
					}
					PdfPCell names = new PdfPCell(new Paragraph(name, tablesN));
					table.addCell(names);

					PdfPCell addresses = new PdfPCell(new Paragraph(address, tablesN));
					table.addCell(addresses);
					PdfPCell trademarks = new PdfPCell(new Paragraph(
							"Coca-Cola\nFanta\nSprite\nNestea\nVitel", tablesN));
					trademarks.setHorizontalAlignment(Element.ALIGN_CENTER);
					table.addCell(trademarks);
					PdfPCell values = new PdfPCell(new Paragraph(current_amount.get(0)
							+ "\n" + current_amount.get(1) + "\n"
							+ current_amount.get(2) + "\n" + current_amount.get(3)
							+ "\n" + current_amount.get(4), tablesN));
					values.setHorizontalAlignment(Element.ALIGN_CENTER);
					table.addCell(values);
					
					PdfPCell maximum = new PdfPCell(new Paragraph(max_amount.get(0)
							+ "\n" + max_amount.get(1) + "\n"
							+ max_amount.get(2) + "\n" + max_amount.get(3)
							+ "\n" + max_amount.get(4), tablesN));
					maximum.setHorizontalAlignment(Element.ALIGN_CENTER);
					
					table.addCell(maximum);
					
					tableEmpty.addCell(names);
					tableEmpty.addCell(trademarks);
					ArrayList<Integer> empty = new ArrayList<Integer>();
					for (int i = 0; i < 5; i++) {
						int max = (int) max_amount.get(i);
						int min = (int) current_amount.get(i);
						int wynik = max - min;
						empty.add(wynik);
					}
					PdfPCell empties = new PdfPCell(new Paragraph(empty.get(0) + "\n"
							+ empty.get(1) + "\n" + empty.get(2) + "\n"
							+ empty.get(3) + "\n" + empty.get(4), tablesN));
					empties.setHorizontalAlignment(Element.ALIGN_CENTER);
					tableEmpty.addCell(empties);
					int emptyAll = empty.get(0) + empty.get(1) + empty.get(2)
							+ empty.get(3) + empty.get(4);
					String temp = String.valueOf("\n\n" + emptyAll);
					PdfPCell emptiesSum = new PdfPCell(new Paragraph(temp, tablesN));
					emptiesSum.setHorizontalAlignment(Element.ALIGN_CENTER);
					tableEmpty.addCell(emptiesSum);

					current_amount.clear();
					max_amount.clear();
					empty.clear();

				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
		
		        Font subFont = new Font(bf, 16, Font.BOLD); 
			Paragraph subPara = new Paragraph("Stany Automatów", subFont);
			addEmptyLine(subPara, 2);
			subCatPart.add(subPara);
			subCatPart.add(table);
			Paragraph space = new Paragraph("", subFont);
			addEmptyLine(space, 3);
			subCatPart.add(space);
			Paragraph subParas = new Paragraph("Braki Automatów", subFont);
			addEmptyLine(subParas, 2);
			subCatPart.add(subParas);
			subCatPart.add(tableEmpty);
			}catch(Exception e){
				e.printStackTrace();
			}

	  	  }
	  
	  
	  
	 private static void createReportOfEmpties(Section subCatPart) throws
	  BadElementException { 
		 try{
		 BaseFont bf = BaseFont.createFont(BaseFont.TIMES_ROMAN,  
                 BaseFont.CP1250, BaseFont.EMBEDDED);   
		 Font table = new Font(bf, 12, Font.BOLD);
		 Font tablesN = new Font(bf, 12, Font.NORMAL);
		PdfPTable tableEmpty = new PdfPTable(5);

		PdfPCell c2 = new PdfPCell(new Paragraph("Nazwa Automatu", table));
		c2.setHorizontalAlignment(Element.ALIGN_CENTER);
		tableEmpty.addCell(c2);
		
		c2 = new PdfPCell(new Paragraph("Adres", table));
		c2.setHorizontalAlignment(Element.ALIGN_CENTER);
		tableEmpty.addCell(c2);

		c2 = new PdfPCell(new Paragraph("Towary", table));
		c2.setHorizontalAlignment(Element.ALIGN_CENTER);
		tableEmpty.addCell(c2);

		c2 = new PdfPCell(new Paragraph("Braki", table));
		c2.setHorizontalAlignment(Element.ALIGN_CENTER);
		tableEmpty.addCell(c2);

		c2 = new PdfPCell(new Paragraph("Braki łączne", table));
		c2.setHorizontalAlignment(Element.ALIGN_CENTER);
		tableEmpty.addCell(c2);

		tableEmpty.setHeaderRows(1);

			
			ArrayList<Integer> current_amount = new ArrayList<Integer>();
			ArrayList<Integer> max_amount = new ArrayList<Integer>();
			ArrayList<String> trademarks_names = new ArrayList<String>();
			try {
				Connection connection = new Connector().getConnection();
				Statement statement = connection.createStatement();
				Statement statement2 = connection.createStatement();
				ResultSet result = statement
						.executeQuery("SELECT * FROM automats.machines order by id");

				while (result.next()) {
					String name = result.getString("machines.name");
					String address = result.getString("machines.address");

					int id = Integer.parseInt(result.getString("machines.id"));

					ResultSet result2 = statement2
							.executeQuery("select machines_trademarks.current_amount, machines_trademarks.max_amount, dictionary_trademarks.name, machines_trademarks.machine_id from automats.machines_trademarks join automats.dictionary_trademarks on machines_trademarks.trademark_id=dictionary_trademarks.id where machines_trademarks.machine_id="
									+ id + " and machines_trademarks.current_amount<10");
					while (result2.next()) {

						current_amount.add(result2
								.getInt("machines_trademarks.current_amount"));
						max_amount.add(result2
								.getInt("machines_trademarks.max_amount"));
						trademarks_names.add(result2.getString("dictionary_trademarks.name"));
					}
					PdfPCell names = new PdfPCell(new Paragraph(name, tablesN));
					PdfPCell addresses = new PdfPCell(new Paragraph(address, tablesN));
					String trade_name="";
					for(int i=0;i<trademarks_names.size();i++){
						trade_name+=trademarks_names.get(i)+"\n";
					}
					PdfPCell trademarks = new PdfPCell(new Paragraph(trade_name, tablesN));
					trademarks.setHorizontalAlignment(Element.ALIGN_CENTER);
					
				
				

					tableEmpty.addCell(names);
					tableEmpty.addCell(addresses);
					tableEmpty.addCell(trademarks);
					ArrayList<Integer> empty = new ArrayList<Integer>();
					for (int i = 0; i < current_amount.size(); i++) {
						int max = (int) max_amount.get(i);
						int min = (int) current_amount.get(i);
						int wynik = max - min;
						empty.add(wynik);
					}
					String pusto="";
					int suma=0;
					for(int i=0;i<empty.size();i++){
						pusto+=empty.get(i) + "\n";
						suma+=empty.get(i);
					}
					PdfPCell empties = new PdfPCell(new Paragraph(pusto, tablesN));
					empties.setHorizontalAlignment(Element.ALIGN_CENTER);
					tableEmpty.addCell(empties);
					
					String temp = String.valueOf("\n" + suma);
					PdfPCell emptiesSum = new PdfPCell(new Paragraph(temp, tablesN));
					emptiesSum.setHorizontalAlignment(Element.ALIGN_CENTER);
					tableEmpty.addCell(emptiesSum);

					current_amount.clear();
					max_amount.clear();
					empty.clear();
					trademarks_names.clear();

				}
			} catch (SQLException e) {
				e.printStackTrace();
			}

			
			 
		    Font subFont = new Font(bf, 16, Font.BOLD); 
			Paragraph subParas = new Paragraph("", subFont);
		
			subCatPart.add(subParas);
			subCatPart.add(tableEmpty);
		 }catch(Exception e){
			 e.printStackTrace();
		 }

	  
	  }
	 

	private static void addContentForAll(Document document)
			throws DocumentException {
	
		try{
			BaseFont bf = BaseFont.createFont(BaseFont.TIMES_ROMAN,  
		                BaseFont.CP1250, BaseFont.EMBEDDED);  
		        Font subFont = new Font(bf, 16, Font.BOLD); 
		        Font catFont = new Font(bf, 18, Font.BOLD); 
		Anchor anchor = new Anchor("Raporty", catFont);
		anchor.setName("Raporty");
		Chapter catPart = new Chapter(new Paragraph(anchor), 1);
		Paragraph subPara = new Paragraph(
				"Stany wszystkich automatów i ich braki", subFont);
		Section subCatPart = catPart.addSection(subPara);
		addEmptyLine(subPara, 2);
		subCatPart.add(new Paragraph());
		createReportOfAllMachines(subCatPart);
		document.add(catPart);
			}catch(Exception e){
				e.printStackTrace();
			}

	}
	
	private static void addContentForEmpties(Document document)
			throws DocumentException {
		try{
			BaseFont bf = BaseFont.createFont(BaseFont.TIMES_ROMAN,  
	                    BaseFont.CP1250, BaseFont.EMBEDDED);  
	        Font subFont = new Font(bf, 16, Font.BOLD); 
	        Font catFont = new Font(bf, 18, Font.BOLD); 
		Anchor anchor = new Anchor("Raporty", catFont);
		anchor.setName("Raporty");
		Chapter catPart = new Chapter(new Paragraph(anchor), 1);
		Paragraph subPara = new Paragraph(
				"Braki w automatach", subFont);
		Section subCatPart = catPart.addSection(subPara);
		addEmptyLine(subPara, 2);
		subCatPart.add(new Paragraph());
		createReportOfEmpties(subCatPart);
		document.add(catPart);
		}catch(Exception e){
			e.printStackTrace();
		}

	}
	
	private static void addContentForSelected(Document document, int machineID)
			throws DocumentException {
		try{
			BaseFont bf = BaseFont.createFont(BaseFont.TIMES_ROMAN,  
	                    BaseFont.CP1250, BaseFont.EMBEDDED);  
	        Font subFont = new Font(bf, 16, Font.BOLD); 
	        Font catFont = new Font(bf, 18, Font.BOLD); 
		Anchor anchor = new Anchor("Raporty", catFont);
		anchor.setName("Raporty");
		Chapter catPart = new Chapter(new Paragraph(anchor), 1);
		Paragraph subPara = new Paragraph(
				"Raport dla wybranego automatu", subFont);
		Section subCatPart = catPart.addSection(subPara);
		addEmptyLine(subPara, 2);
		subCatPart.add(new Paragraph());
		createReportOfSelected(subCatPart, machineID);
		document.add(catPart);
		}catch(Exception e){
			e.printStackTrace();
		}

	}

	public static String generateRaportForAllAutomats(int userID, String type) {
		try {
			Document document = new Document();
			String output = "c:/temp/Raport_dla_wszystkich_automatow"+userID+".pdf";
			PdfWriter.getInstance(document, new FileOutputStream(output));
			document.open();
			addMetaData(document);
			addTitlePage(document, userID, type);
			addContentForAll(document);
			document.close();

			return "Wygenerowano raport";

		} catch (Exception e) {
			e.printStackTrace();
		}
		return "Blad przy generowaniu raportu";
	}
	
	public static String generateRaportForEmptiesAutomats(int userID, String type) {
		try {
			Document document = new Document();
			String output = "c:/temp/Raport_dla_pustych_automatow"+userID+".pdf";
			PdfWriter.getInstance(document, new FileOutputStream(output));
			document.open();
			addMetaData(document);
			addTitlePage(document, userID, type);
			addContentForEmpties(document);
			document.close();

			return "Wygenerowano raport";

		} catch (Exception e) {
			e.printStackTrace();
		}
		return "Błąd przy generowaniu raportu";
	}
	
	public static String generateRaportForSelectedAutomats(int userID, int machineID, String type) {
		try {
			if(machineID==0){
				return "Nie wybrano maszyny!";
			}else{
			Document document = new Document();
			String output = "c:/temp/Raport_dla_wybranego_automatu"+userID+".pdf";
			PdfWriter.getInstance(document, new FileOutputStream(output));
			document.open();
			addMetaData(document);
			addTitlePage(document, userID, type);
			addContentForSelected(document, machineID);
			document.close();

			return "Wygenerowano raport";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "Błąd przy generowaniu raportu";
	}
	
	
	public static String getCreationDate(int userID, String type){
		File file;
		if(type.equals("all")){
			file = new File("c:/temp/Raport_dla_wszystkich_automatow"+userID+".pdf");
		}else if(type.equals("empty")){
			file = new File("c:/temp/Raport_dla_pustych_automatow"+userID+".pdf");
		}else if(type.equals("selected")){
			file = new File("c:/temp/Raport_dla_wybranego_automatu"+userID+".pdf");
		}else{
			return "Plik nie istnieje";
		}
		if(file.exists()){
			SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
			String lastChange=format.format(file.lastModified());
			return lastChange;
		}else
			return "Plik nie istnieje";
	}
	
	public static String readFile(int userID, String type){
		try{
			File pdfFile;
		if(type.equals("all")){
		pdfFile = new File("c:/temp/Raport_dla_wszystkich_automatow"+userID+".pdf");
		}else if(type.equals("empty")){
			pdfFile = new File("c:/temp/Raport_dla_pustych_automatow"+userID+".pdf");
		}else if(type.equals("selected")){
			pdfFile = new File("c:/temp/Raport_dla_wybranego_automatu"+userID+".pdf");
		}else{
			return "Plik nie istnieje";
		}
		if (pdfFile.exists()) {
 
			if (Desktop.isDesktopSupported()) {
				Desktop.getDesktop().open(pdfFile);
				return "Raport odczytany";
			} else {
				return "Awt Desktop nie jest wspierany";
			}
 
		} else {
			return "Plik nie istnieje";
		}

	  } catch (Exception ex) {
		ex.printStackTrace();
	  }
		return "Gotowe";
	}
	
}


